package models

import (
	"errors"
	"gitee.com/lzqysoft/framework/common/tool"
	"time"
	"gitee.com/lzqysoft/framework/common/uuid"
)

type Profile struct {
	Id           string `xorm:"not null pk index unique VARCHAR(64)"`
	Onwer        string `xorm:"VARCHAR(64)"`
	Group        string `xorm:"VARCHAR(64)"`
	Creater      string `xorm:"VARCHAR(64)"`
	CreateAt     int64  `xorm:"BIGINT"`
	CreateOrg    string `xorm:"VARCHAR(64)"`
	Updater      string `xorm:"VARCHAR(64)"`
	UpdateAt     int64  `xorm:"BIGINT"`
	Deleter      string `xorm:"VARCHAR(64)"`
	DeleteAt     int64  `xorm:"BIGINT"`
	Isdelete     string `xorm:"VARCHAR(2)"`
	Status       string `xorm:"VARCHAR(2)"`
	Operation    int    `xorm:"INTEGER"`
	Remark       string `xorm:"VARCHAR(255)"`
	Obligate3    string `xorm:"VARCHAR(64)"`
	Obligate2    string `xorm:"VARCHAR(64)"`
	Obligate     string `xorm:"VARCHAR(64)"`
	UserId       string `xorm:"not null VARCHAR(64)"`
	Portrait     string `xorm:"VARCHAR(255)"`
	Mail         string `xorm:"VARCHAR(32)"`
	Phone        string `xorm:"VARCHAR(32)"`
	Name         string `xorm:"VARCHAR(32)"`
	Idcard       string `xorm:"VARCHAR(32)"`
	Sex          string `xorm:"VARCHAR(2)"`
	ProvinceCode string `xorm:"VARCHAR(16)"`
	CityCode     string `xorm:"VARCHAR(16)"`
	CountyCode   string `xorm:"VARCHAR(16)"`
	Province     string `xorm:"VARCHAR(255)"`
	City         string `xorm:"VARCHAR(255)"`
	County       string `xorm:"VARCHAR(255)"`
	Address      string `xorm:"VARCHAR(512)"`
	Occupation   string `xorm:"VARCHAR(32)"`
	Prat         string `xorm:"VARCHAR(32)"`
	Qq           string `xorm:"VARCHAR(32)"`
	Wechat       string `xorm:"VARCHAR(32)"`
	Nick         string `xorm:"VARCHAR(32)"`
	Desc         string `xorm:"VARCHAR(255)"`
}

func (v *Profile) TableName() string {
	return "base_profile"
}
func (v *Profile) BeforeInsert(){
	v.Id = uuid.UUID()
	v.CreateAt = time.Now().Unix()
	v.UpdateAt = time.Now().Unix()
}
func (v Profile) Convert2Map() map[string]interface{} {
	return tool.Struct2Map(v)
}

func GetProfileById(id string) (*Profile, error) {
	profile := &Profile{Id: id}
	has, err := db.Get(profile)
	if !has {
		return nil, errors.New("no record")
	}
	if err != nil {
		return nil, err
	}
	return profile, err
}

func GetProfileByUserId(userId string) (*Profile, error) {
	profile := &Profile{UserId: userId}
	has, err := db.Get(profile)
	if !has {
		return nil, errors.New("no record")
	}
	if err != nil {
		return nil, err
	}
	return profile, err
}

func AddProfile(profile *Profile) error {
	_, err := db.Insert(profile)
	return err
}